package com.sskj.hhrt.mine;


import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hey.lib.HeySpinner;
import com.hjq.toast.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sskj.base.BuildConfig;
import com.sskj.base.data.User;
import com.sskj.base.simple.SimpleTextWatcher;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.utils.MoneyValueFilter;
import com.sskj.base.utils.NumberUtil;
import com.sskj.base.view.ToolBarLayout;
import com.sskj.hhrt.MVPActivity;
import com.sskj.hhrt.MyApplication;
import com.sskj.hhrt.R;
import com.sskj.hhrt.WebViewActivity;
import com.sskj.hhrt.api.SystemInfo;
import com.sskj.hhrt.mine.data.BankBean;
import com.sskj.hhrt.mine.data.PayWayBean;
import com.sskj.hhrt.mine.data.RechargeBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 入金--充值
 *
 * @author Hey
 * Create at  2019/03/27
 */
public class RechargeActivity extends MVPActivity<RechargePresenter> {

    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.availableText)
    TextView availableText;
    @BindView(R.id.bank_spinner)
    HeySpinner bankSpinner;
    @BindView(R.id.edt_price)
    EditText edtPrice;
    @BindView(R.id.totalPriceText)
    TextView totalPriceText;
    @BindView(R.id.submit)
    Button submit;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    private double usdRate;
    private List<String> bankNames = new ArrayList<>();
    private List<BankBean> bankData = new ArrayList<>();
    private String selectBankCode;
    private double minRecharge;

    private boolean bankLoad = true, rateLoad;

    @Override
    public int getLayoutId() {
        return R.layout.activity_recharge;
    }

    @Override
    public RechargePresenter getPresenter() {
        return new RechargePresenter();
    }

    @Override
    public void initView() {
        bankSpinner.setOnSelectListener(position -> {
            selectBankCode = bankData.get(position).getBankCardTypeCode();
        });
        toolbar.setLeftButtonOnClickListener(v -> finish());

        ClickUtil.click(submit, view -> {
//            if (!bankLoad) {
//                ToastUtils.show("数据加载失败，请下拉刷新后重试");
//                return;
//            }

            if (isEmpty(edtPrice)) {
                ToastUtils.show("请输入入金金额");
                return;
            }


//            if (Double.parseDouble(getText(totalPriceText)) < minRecharge) {
//                ToastUtils.show("入金金额最小为$" + minRecharge);
//                return;
//            }

            mPresenter.getPayWay(getText(edtPrice));

//            String url = BuildConfig.BASE_URL + "app/interface?app=app" +
//                    "&account=" + account +
//                    "&action=datongOrderPay" +
//                    "&sessionId=" + MyApplication.session +
//                    "&systemType=ANDROID" +
//                    "&fee=" + getText(edtPrice) +
//                    "&usdFee=" + getText(totalPriceText) +
//                    "&bankCode=" + selectBankCode +
//                    "&payType=5" +
//                    "&payWayId=13";
//            WebViewActivity.start(this, url);
//            mPresenter.xinghuoPay(account, getText(edtPrice), getText(totalPriceText), selectBankCode);
        });
        edtPrice.setFilters(new InputFilter[]{new MoneyValueFilter(2)});
//        edtPrice.addTextChangedListener(new SimpleTextWatcher() {
//            @Override
//            public void afterTextChanged(Editable s) {
//
//                if (!rateLoad) {
//                    ToastUtils.show("数据加载失败，请下拉刷新后重试");
//                    return;
//                }
//
//                if (!TextUtils.isEmpty(s)) {
//                    double cny = Double.parseDouble(s.toString());
//                    double udt = cny / usdRate;
//                    setText(totalPriceText, NumberUtil.keepHalfMax2(udt));
//                } else {
//                    setText(totalPriceText, "---");
//                }
//            }
//        });
    }

    @Override
    public void initData() {
        mPresenter.getSystemInfo();
//        mPresenter.getPayWay(account);
//        mPresenter.getBank(account);
        initRefreshLayout(smartRefreshLayout, false);
        smartRefreshLayout.setOnRefreshListener(refreshLayout -> {
//            mPresenter.getBank(account);
            mPresenter.getSystemInfo();
        });

//        bankSpinner.setClickListener(() -> {
//            if (bankSpinner.getAdapter() == null) {
//                mPresenter.getBank(account);
//                return true;
//            }
//            return false;
//        });
    }

    public void stopRefresh() {
        smartRefreshLayout.finishRefresh();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getAccountDetail(account, true);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, RechargeActivity.class);
        context.startActivity(intent);
    }



    public void setBankData(List<BankBean> data) {
        if (data != null) {
            bankLoad = true;
            bankData = data;
            for (int i = 0; i < data.size(); i++) {
                bankNames.add(data.get(i).getBankCardType());
            }
            bankSpinner.attachData(bankNames);
        }
    }

    public void setSysInfo(SystemInfo data) {
        rateLoad = true;
        usdRate = data.getInRate();
        minRecharge = data.getMinInCharge();
    }

    public void onPaySuccess(String data) {
        WebViewActivity.start(this, data);
    }

    public void setAccountInfo(User data) {
        // 房 ---隐藏
        setText(availableText, data.getWallone());
    }
}
